﻿# define IsNotEnableChechUserRight
# undef IsNotEnableChechUserRight
//创建时间: 2010-12-19
//创建作者: 李城
//功能说明: CMS系统 用户登录验证 
using System;
using System.Collections.Generic;
using System.Text;
using TRPOP.Common;
using TRPOP.CMS.Model;
using System.Data;
using TRPOP.CMS.Business;
using TRPOP.CMS.Plugin;
using System.Web;

namespace TRPOP.CMS.UserManage
{
    public class AdminManage : IAdminUser
    {
        UserBaseManage mUserBaseManage = new UserBaseManage();
        const string mstrAdministratorship = "Administratorship";
        const string strCookiesName = "yorkcms";
        const string strCookiesDecodePasswordKey = @"D3F51SD2S";
        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <param name="strErrorInfo"></param>
        /// <returns></returns>
        public bool AdminLogin(string strUserName, string strPassword)
        {
            strUserName = strUserName.Trim().SQLFilter();
            strPassword = strPassword.Trim().MD5Normal();
            List<UserBaseInfo> olist = mUserBaseManage.GetListByCondition(1, " and  [LogName]='" + strUserName + "'", "");
            if (olist != null && olist.Count > 0 && olist[0].LogPWD == strPassword)
            {
                SaveCurrentInfo(olist[0]);
                UserBaseInfo oUserBaseInfo = olist[0];
                oUserBaseInfo.LastLogTime = DateTime.Now;
                mUserBaseManage.Save(oUserBaseInfo);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检验用户是否登录
        /// </summary>
        /// <returns></returns>
        public bool CheckLogin()
        {
            bool blnReturn = true;

#if IsNotEnableChechUserRight
                        blnReturn=true;
#else
            blnReturn = (getUserBaseInfo() != null && getUserBaseInfo() is UserBaseInfo);
#endif

            return blnReturn;
        }

        /// <summary>
        /// 获取用户登录的信息
        /// </summary>
        /// <returns></returns>
        public object getUserBaseInfo()
        {
#if IsNotEnableChechUserRight
                return getUserBaseInfo(1);
#else
            return GetCurrentInfo();
#endif
        }

        /// <summary>
        /// 根据用户id获取用户名
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public string GetAdminNameById(int intUserId)
        {
            UserBaseInfo oUserBaseInfo = getUserBaseInfo(intUserId) as UserBaseInfo;
            if (oUserBaseInfo != null)
                return oUserBaseInfo.Name;
            return "";
        }

        /// <summary>
        /// 获取用户登录的信息
        /// </summary>
        /// <returns></returns>
        public object getUserBaseInfo(int intUserId)
        {
            return mUserBaseManage.GetInfoById(intUserId);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public object GetCurrentInfo()
        {
            return System.Web.HttpContext.Current.Session[mstrAdministratorship];
        }
        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="obj"></param>
        public void SaveCurrentInfo(object obj)
        {
            System.Web.HttpContext.Current.Session[mstrAdministratorship] = obj;
            System.Web.HttpContext.Current.Session.Timeout = 20;
        }

        /*
         * 代码开始  2011.01.08 李城
        */ 
        /// <summary>
        /// 根据验证码枚举值判断用户权限
        /// </summary>
        /// <param name="oPermissionKeys"></param>
        /// <returns></returns>
        public bool CheckUserPermission(PermissionKeys.PermissionEnum oPermissionKeys)
        {
            return true;
        }

        /// <summary>
        /// 根据验证码枚举值判断用户权限
        /// </summary>
        /// <param name="oPermissionKeys"></param>
        /// <returns></returns>
        public bool CheckUserPermission(string oPermissionKeys)
        {
            return true;
        }

        public void LoginOut()
        {
            HttpContext.Current.Session.Abandon();
            return;
        }
        public List<object> GetPermissionListByParentId(int intParentid)
        {
            return new List<object>();
        }
        /// <summary>
        /// 获取当前用户可操作的所有节点。
        /// 格式如下：1,2,3,4,5,6 。 如果全部，则为all。
        /// </summary>
        /// <returns></returns>
        public string GetEnabledNodeIds()
        {
            return "all";
        }
        /// <summary>
        /// 保存当前用户信息  
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveUserInfo(object obj)
        {
            return new UserBaseManage().Save(obj as UserBaseInfo) == EnumSubmitResult.Success;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="intUserID"></param>
        /// <returns></returns>
        public bool DeleteUserInfoById(int intUserID)
        {
            return true;
        }
        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <param name="topn"></param>
        /// <param name="condition"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public List<object> GetListByConditionForRealPubCount(int topn, string condition, string orderby)
        {
            return new List<object>();
        }

        public List<object> GetUserListByCondition(int topn, string condition, string orderby)
        {
            return new List<object>();
        }
        /// <summary>
        /// 给当前用户增加节点权限
        /// </summary>
        /// <param name="intNodeId"></param>
        /// <returns></returns>
        public bool AddPermissionByNodeId(int intNodeId)
        {
            return true;
        }
        /*
         * 代码结束  2011.01.08 李城
        */

        #region 创建Cookies
        /// <summary>
        /// 创建Cookies
        /// </summary>
        /// <param name="userInfo"></param>
        public static void CerateCookies(int userid, string paswd)
        {

            HttpCookie cookie = new HttpCookie(strCookiesName);
            cookie.Values["userid"] = userid.ToString();
            cookie.Values["password"] = VS.Tools.Encryption.DES.Encode(paswd, strCookiesDecodePasswordKey).UrlEncode();

            string strCookieDomain = HttpContext.Current.Request.Url.Authority;
            strCookieDomain = strCookieDomain.Substring(strCookieDomain.IndexOf('.') + 1);
            cookie.Domain = strCookieDomain;

            HttpContext.Current.Response.AppendCookie(cookie);

        }
        /// <summary>
        /// 创建Cookies并设置时间
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="paswd"></param>
        /// <param name="expires"></param>
        public static void CerateCookies(int userid, string paswd, DateTime expires)
        {

            HttpCookie cookie = new HttpCookie(strCookiesName);
            cookie.Values["userid"] = userid.ToString();
            cookie.Values["password"] = VS.Tools.Encryption.DES.Encode(paswd, strCookiesDecodePasswordKey).UrlEncode();

            string strCookieDomain = HttpContext.Current.Request.Url.Authority;
            strCookieDomain = strCookieDomain.Substring(strCookieDomain.IndexOf('.') + 1);
            cookie.Domain = strCookieDomain;
            cookie.Expires = expires;
            HttpContext.Current.Response.AppendCookie(cookie);

        }
        #endregion

        #region 读Cookie
        /// <summary>
        /// 读Cookie
        /// </summary>
        /// <returns></returns>
        public HttpCookie GetAdminCookie()
        {
            HttpContext ocContext = HttpContext.Current;
            HttpCookie ohcCookie = ocContext.Request.Cookies[strCookiesName];
            /*
               HttpUtility.UrlDecode(ohcCookie.Values["password"].ToString());
                int.Parse(ohcCookie.Values["userid"].ToString());
             */
            return ohcCookie;
        }
        /// <summary>
        /// 读Cookie中的密码
        /// </summary>
        /// <param name="hcCookie"></param>
        /// <returns></returns>
        public string DecodeCookiePwd(HttpCookie hcCookie)
        {
            string strDecodePwd = "";
            if (hcCookie != null)
            {
                try
                {
                    string strPwd = HttpUtility.UrlDecode(hcCookie.Values["password"].ToString());
                    strDecodePwd = VS.Tools.Encryption.DES.Decode(strPwd, strCookiesDecodePasswordKey);
                }
                catch { return ""; }
            }
            else
            {
                return "";
            }           
            return strDecodePwd;
        }

        #endregion

    }
}
